<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/5/10
 * Time: 下午3:27
 */

namespace App\Busi\Modules\Resource;


use App\Busi\Modules\BaseBusi;
use App\Busi\Traits\CategoryTrait;
use App\Busi\Traits\CompanyTrait;
use App\Busi\Traits\DistTrait;
use App\Http\Requests\CompanyDetailRo;
use App\Models\Company;
use App\Models\CompanyCase;
use App\Models\CompanyStaff;
use App\Models\SecQualifyCategory;

class CompanyDetailBusi extends BaseBusi
{
    use CompanyTrait,CategoryTrait;
    public function handle(CompanyDetailRo $ro){
        $id = $ro->id;
        $where = [
            ['id','=',$id]
        ];
        $data = Company::where($where)->first();
        $hearinfo['company_name'] = $data['name'];
        $hearinfo['logo'] = config('admin.upload.host').$data['logo'];
        $hearinfo['star'] = $data['star'];
        $hearinfo['view_num'] = $data['view_num'];
        $province = $this->getDistNameid($data["province_id"]);
        $city = $this->getDistNameid($data["city_id"]);
        $hearinfo["address"] = $province.$city.$data["address"];

        $ablum = $data['pictures'];
        if($ablum){
            foreach ($ablum as &$item){
                $item = config('admin.upload.host').$item;
            }
        }


        $category = " ";
        $subcateroys = $data->subcategorys;
        $categoryList = [];
        if($subcateroys != null){
            foreach ($subcateroys as &$cate){
                array_push($categoryList,$cate["category_name"]);
            }

        }


        $ret['id'] =$data['id'];


        $ret['hearinfo'] =$hearinfo;

        $ret['album'] =$ablum;
        $ret['desc'] =$data['desc'];

        $ret['company_desc'] =$data['desc'];
        $ret['licence_poster'] =config("admin.upload.host").$data['licence_poster'];
        $ret['contact_name'] =$data['contact_name'];
        $ret['contact_number'] =$data['contact_number'];

        $ret['scale'] =$data['scale'];





        $ret['hearinfo'] =$hearinfo;

        $ret['scale'] =$data['scale'];


        $ret['categorys'] =$categoryList;



        $ret['advantage'] = $data['advantage'];

        $ret['company_case'] = $this->getCompanyCaseById($id);
        $ret['company_staff'] = $this->getCompanyStaff($id);
        return $ret;

    }
    public function getCompanyCaseById($id){
        $where = [
            ['company_id','=',$id]
        ];
        $fields = ['id','poster','company_name','category_id','service_desc','project_cycle','project_price','staff','is_hidden'];
        $data = CompanyCase::where($where)->select($fields)->get();
        foreach ($data as $item){
            $item['poster'] = config("admin.upload.host").$item['poster'];
            $item['subcategory_id'] = $item['category_id'];
            $item['supcategory_id'] = $this->getSupCateIdById($item['category_id']);

            $item['category_name'] = $this->getFullCategoryNameByID($item['category_id']);

            unset($item['category_id']);

        }
        return $data;
    }
    public function getCompanyStaff($id){
        $where = [
            ['company_id','=',$id]
        ];
        $fields = ['id','phone','name','avatar','experience','is_hidden'];
        $data = CompanyStaff::where($where)->select($fields)->get();
        foreach ($data as $item){
            $item['avatar'] = config("admin.upload.host").$item['avatar'];
        }
        return $data;
    }


}